package com.zwj.batch.core.batch.step.reader;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zwj.batch.domain.po.UserPO;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.json.JacksonJsonObjectReader;
import org.springframework.batch.item.json.JsonItemReader;
import org.springframework.batch.item.json.builder.JsonItemReaderBuilder;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;


/**
 * @author 张文接
 * @version 1.0
 * @createTime 2023-06-26 11:07:00
 */
@Component
public class UserReader {
    /**
     * 自定义映射逻辑
     *
     * @return FieldSetMapper
     */
    private static FieldSetMapper<UserPO> getUserFieldSetMapper() {
        return fieldSet -> {
            UserPO userPO = new UserPO();
            userPO.setId(fieldSet.readLong("id"));
            userPO.setAge(fieldSet.readInt("age"));
            userPO.setName(fieldSet.readString("name"));
            String addr = fieldSet.readString("province") + " " + fieldSet.readString("city")
                    + " " + fieldSet.readString("area");
            userPO.setAddress(addr);
            return userPO;
        };
    }

    /**
     * 读取 user.txt 文件
     *
     * @return step
     */
    public FlatFileItemReader<UserPO> userItemReader(String path) {
        //FlatFileItemReader spring batch 平面文件读入类
        //特点：一行一行的读数据
        FlatFileItemReaderBuilder<UserPO> builder = new FlatFileItemReaderBuilder<>();
        return builder.name("userItemReader")
                .resource(new ClassPathResource(path))//指定读取的文件
                .delimited().delimiter("#")//读取出一行数据，该如何分割数据，默认以,分割，当前使用#号分割
                .names("id", "name", "age")//给分割后数据打name标记，后续跟User对象属性进行映射
                .targetType(UserPO.class)//读取出一行数据封装成什么对象
                .build();
    }

    /**
     * 实现自定义映射逻辑的平面文件读取
     *
     * @return step
     */
    public FlatFileItemReader<UserPO> userItemReader2() {
        FieldSetMapper<UserPO> userFieldSetMapper = getUserFieldSetMapper();
        return new FlatFileItemReaderBuilder<UserPO>()
                .name("userItemReader2")
                .resource(new ClassPathResource("User.txt"))//获取待处理的文件
                .delimited().delimiter("#")//设置字段分隔符为#
                .names("id", "name", "age", "province", "city", "area")//解析对应的字段标识
                .fieldSetMapper(userFieldSetMapper) //添加自定义映射逻辑
                .build();
    }

    /**
     * 读取 json 文件
     *
     * @return ItemReader
     */
    public JsonItemReader<UserPO> userItemReader3() {
        //Jackson 解析器
        ObjectMapper objectMapper = new ObjectMapper();
        JacksonJsonObjectReader<UserPO> jsonObjectReader = new JacksonJsonObjectReader<>(UserPO.class);
        jsonObjectReader.setMapper(objectMapper);
        return new JsonItemReaderBuilder<UserPO>()
                .name("userJsonItemReader")
                .jsonObjectReader(jsonObjectReader) //设置 json 解析器
                .resource(new ClassPathResource("users.json"))
                .build();
    }
}
